package cn.itcast.wushangxiantu.Fragment.jiaoyisuo;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;

import java.io.IOException;

import cn.itcast.wushangxiantu.BaseAdapter.JiaoYiSuoWoDeShangJiaAdapter;
import cn.itcast.wushangxiantu.UserData.JiaoYiSuoShangjiayulan;
import cn.itcast.wushangxiantu.UserData.User;
import cn.itcast.wushangxiantu.Activity.Login;
import cn.itcast.wushangxiantu.OkhttpUtil;
import cn.itcast.wushangxiantu.R;
import cn.itcast.wushangxiantu.ToastUtil;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

@SuppressLint("ValidFragment")
public class JiaoYiSuoShangjia extends Fragment {
    private View view;
    private ListView lv;
    JiaoYiSuoWoDeShangJiaAdapter adapter;
    private Context context;
    JiaoYiSuoShangjiayulan jiaoYiSuoShangjia;
    int num;

    public JiaoYiSuoShangjia(Context context) {
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_jiaoyisuo_wodeshangjia, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        lv = (ListView) view.findViewById(R.id.lv);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                num = i;
                String content = "";
                content = "武器名称；" + jiaoYiSuoShangjia.getWP().get(i).getName() + "\n" +
                        "使用等级：" + jiaoYiSuoShangjia.getWP().get(i).getSydj() + "\n******装备属性******\n";
                switch (jiaoYiSuoShangjia.getWP().get(i).getRegion()) {
                    case 1:
                        content = "道具名称：" + jiaoYiSuoShangjia.getWP().get(i).getName() + "\n******属性******\n" +
                                "说明：" + jiaoYiSuoShangjia.getWP().get(i).getShuoming() + "\n";
                        break;
                    case 2:
                        content += "攻击力：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 8 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 50))) + "\n";
                        if (((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() / 5 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 1000))) > 45) {
                            content += "暴击率：45\n";
                        } else {
                            content += "暴击率：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() / 5 +
                                    jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 1000))) + "\n";

                        }
                        break;
                    case 3:
                        content += "生命：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 30 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 50))) + "\n";
                        content += "防御力：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 4 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 100))) + "\n";
                        break;
                    case 4:
                        if (((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() / 5 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 1000))) > 45) {
                            content += "命中：45\n";
                        } else {
                            content += "命中：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() / 5 +
                                    jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 1000))) + "\n";

                        }
                        content += "穿透：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 2 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 50))) + "\n";
                        break;
                    case 5:
                        if (((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() / 5 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 1000))) > 45) {
                            content += "闪避：45\n";
                        } else {
                            content += "闪避：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() / 5 +
                                    jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 1000))) + "\n";
                        }

                        content += "恢复：" + ((int) (jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 2 +
                                jiaoYiSuoShangjia.getWP().get(i).getJsdj() * (double) (jiaoYiSuoShangjia.getWP().get(i).getPz() / 50))) + "\n";
                        break;
                    case 6:
                        content = "灵石名称：" + jiaoYiSuoShangjia.getWP().get(i).getName() + "\n******属性******\n" +
                                "说明：" + jiaoYiSuoShangjia.getWP().get(i).getShuoming() + "\n******装备属性******\n" +
                                "生命：" + jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 150 + "\n" +
                                "防御力：" + jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 20 + "\n" +
                                "攻击力：" + jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 40 + "\n" +
                                "恢复：" + jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 10 + "\n" +
                                "穿透：" + jiaoYiSuoShangjia.getWP().get(i).getJsdj() * 10 + "\n";
                        break;
                }
                GeneralDialog("当前物品", content);
            }
        });
        getDate();
    }

    private void getDate() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber() + "&need=shangjiayulan", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("shangjia1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("shangjia2", src);
                JiaoYiSuoShangjiayulan jiaoYiSuoShangjia = null;
                try {
                    jiaoYiSuoShangjia = new Gson().fromJson(src, JiaoYiSuoShangjiayulan.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = jiaoYiSuoShangjia;
                message.what = 1;
                handler.sendMessage(message);
            }
        });

    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (message.what == 1) {
                jiaoYiSuoShangjia = (JiaoYiSuoShangjiayulan) message.obj;
                if (jiaoYiSuoShangjia != null) {
                    if (adapter == null) {
                        adapter = new JiaoYiSuoWoDeShangJiaAdapter(context);
                    }
                    adapter.setJiaoYiSuoWoDeShangJia(jiaoYiSuoShangjia);
                    lv.setAdapter(adapter);
                } else {
                    ToastUtil.toast(context, "服务器坏了");
                }
            } else if (message.what == 2) {
                User user = (User) message.obj;
                if (user != null) {
                    if (user.getStw().equals("OK")) {
                        ToastUtil.toast(context, "下架成功");
                        jiaoYiSuoShangjia.getWP().remove(num);
                        adapter.setJiaoYiSuoWoDeShangJia(jiaoYiSuoShangjia);
                    } else {
                        ToastUtil.toast(context, user.getStw());
                    }
                } else {
                    ToastUtil.toast(context, "服务器坏掉了");
                }
            }
            return false;
        }
    });

    public void GeneralDialog(String head, String content) {
        /**
         * 普通对话框
         * 1标题
         * 2内容
         */
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setTitle(head)
                .setMessage(content)
                .setPositiveButton("下架", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        xiajia();
                    }
                });
        dialog = builder.create();
        dialog.show();
    }

    private void xiajia() {
        OkhttpUtil.getRequest(OkhttpUtil.URL_LOG + "userName=" + Login.getUser().getUsernumber() + "&need=xiajia" + "&id=" + jiaoYiSuoShangjia.getWP().get(num).getId(), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.v("xiajia1", e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String src = response.body().string();
                Log.v("xiajia2", src);
                User user = null;
                try {
                    user = new Gson().fromJson(src, User.class);
                } catch (Exception e) {

                }
                Message message = new Message();
                message.obj = user;
                message.what = 2;
                handler.sendMessage(message);
            }
        });


    }


}
